package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.system.api.OperateLogControllerApi;
import com.zdpx.pxframework.system.api.entity.OperateLog;
import com.zdpx.pxframework.system.service.OperateLogService;
import com.zdpx.pxframework.system.vo.query.OperateLogQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;



/**
 * 操作日志 接口控制器
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2022-05-24 13:27:18
 */
@RestController
@RequestMapping("/operate-log")
public class OperateLogController implements OperateLogControllerApi {
    @Resource
    private OperateLogService operateLogService;


    /**
    * 获取条件查询信息
    *
    * @param operateLogQuery 查询条件
    * @return 查询封装
    */
    private LambdaQueryWrapper<OperateLog> getOperateLogQueryWrapper(OperateLogQuery operateLogQuery) {
        LambdaQueryWrapper<OperateLog> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(operateLogQuery.getModuleName())) {
            wrapper.like(OperateLog::getModuleName, operateLogQuery.getModuleName());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getTitle())) {
            wrapper.like(OperateLog::getTitle, operateLogQuery.getTitle());
        }
        if (operateLogQuery.getStatus()!=null) {
            wrapper.eq(OperateLog::getStatus, operateLogQuery.getStatus());
        }
        if (operateLogQuery.getType()!=null) {
            wrapper.eq(OperateLog::getType, operateLogQuery.getType());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getCreator())) {
            wrapper.like(OperateLog::getCreator, operateLogQuery.getCreator());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getUserAgent())) {
            wrapper.like(OperateLog::getUserAgent, operateLogQuery.getUserAgent());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getIp())) {
            wrapper.like(OperateLog::getIp, operateLogQuery.getIp());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getBrowser())) {
            wrapper.like(OperateLog::getBrowser, operateLogQuery.getBrowser());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getOperateSystem())) {
            wrapper.like(OperateLog::getOperateSystem, operateLogQuery.getOperateSystem());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getRequestUri())) {
            wrapper.like(OperateLog::getRequestUri, operateLogQuery.getRequestUri());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getRequestMethod())) {
            wrapper.like(OperateLog::getRequestMethod, operateLogQuery.getRequestMethod());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getRequestArgs())) {
            wrapper.like(OperateLog::getRequestArgs, operateLogQuery.getRequestArgs());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getJavaMethod())) {
            wrapper.like(OperateLog::getJavaMethod, operateLogQuery.getJavaMethod());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getJavaMethodArgs())) {
            wrapper.like(OperateLog::getJavaMethodArgs, operateLogQuery.getJavaMethodArgs());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getResultCode())) {
            wrapper.like(OperateLog::getResultCode, operateLogQuery.getResultCode());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getResultData())) {
            wrapper.like(OperateLog::getResultData, operateLogQuery.getResultData());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getResultMsg())) {
            wrapper.like(OperateLog::getResultMsg, operateLogQuery.getResultMsg());
        }
        if (operateLogQuery.getUseTime()!=null) {
            wrapper.eq(OperateLog::getUseTime, operateLogQuery.getUseTime());
        }
        if (!StringUtils.isEmpty(operateLogQuery.getException())) {
            wrapper.like(OperateLog::getException, operateLogQuery.getException());
        }
        return wrapper;
    }

    /**
    * 按照条件查询操作日志信息
*
    * @param operateLogQuery 查询条件
    * @return 条件结果信息
    */
    @Override
    @GetMapping
    public Result<List<OperateLog>> list(OperateLogQuery operateLogQuery) {
        return Result.success(operateLogService.list(getOperateLogQueryWrapper(operateLogQuery)));
    }

    /**
    * 分页及条件查询操作日志信息
    * @param page 页码
    * @param size 分页大小
    * @param  operateLogQuery 查询条件
    * @return 分页结果信息
    */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<OperateLog>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, OperateLogQuery operateLogQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<OperateLog> pageParam = new Page<>(page, size);

        operateLogService.page(pageParam, getOperateLogQueryWrapper(operateLogQuery));
        PageResult<OperateLog> pageResult = new PageResult<OperateLog>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
    * 根据ID获取操作日志详情
    *
    * @param id 操作日志ID
    * @return 操作日志详细信息
    */
    @Override
    @GetMapping("/{id}")
    public Result<OperateLog> detail(@PathVariable("id") String id) {
        return Result.success(operateLogService.detailById(id));
    }

    /**
    * 增加操作日志信息
    *
    * @param operateLog 操作日志增加实体
    * @return 增加后的操作日志信息
    */
    @Override
    @PostMapping
    public Result<OperateLog> add(@RequestBody @Validated(AddGroup.class) OperateLog operateLog) {
        operateLogService.saveOperateLog(operateLog);
        return Result.success(operateLog);
    }

    /**
    * 修改操作日志信息
    *
    * @param id             操作日志ID
    * @param operateLog 操作日志修改实体
    * @return 修改后的操作日志信息
    */
    @Override
    @PutMapping("/{id}")
    public Result<OperateLog> updateById(@PathVariable("id") String id,@RequestBody @Validated(UpdateGroup.class) OperateLog operateLog) {
        operateLog.setId(id);
        operateLogService.updateOperateLogById(operateLog);
        return Result.success(operateLogService.getById(id));
    }


    /**
    * 删除操作日志信息
    *
    * @param id 操作日志ID
    * @return 删除结果
    */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        operateLogService.deleteById(id);
        return Result.success(true);
    }

    /**
    * 删除操作日志信息
    *
    * @param idList 操作日志ID数组
    * @return 删除结果
    */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        operateLogService.removeByIds(idList);
        return Result.success(true);
    }
}
